﻿using ERP.ERPModels.Production.ProductionManagement;
using ERP.ERPModels.QualityAssurance;
using ERP.QualityAssurance.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace ERP.QualityAssurance
{
    public class TestitemsettingService : ITestitemsettingService
    {
        private readonly IRepository<Testitemsettings, int> _testitemsettingsRepository;

        public TestitemsettingService(IRepository<Testitemsettings, int> testitemsettingsRepository)
        {
            _testitemsettingsRepository = testitemsettingsRepository;
        }

        public async Task<TestitemsettingDto> CreateAsync(CreateTestitemsettingDto input)
        {
            var entity = new Testitemsettings
            {
                ItemSettingsCode = input.ItemSettingsCode,
                ItemSettingsName = input.ItemSettingsName,
                ItemSettingsType = input.ItemSettingsType,
                ItemSettingStool = input.ItemSettingStool,
                Notes = input.Notes
            };
            await _testitemsettingsRepository.InsertAsync(entity);
            return new TestitemsettingDto
            {
                Id = entity.Id,
                ItemSettingsCode = entity.ItemSettingsCode,
                ItemSettingsName = entity.ItemSettingsName,
                ItemSettingsType = entity.ItemSettingsType,
                ItemSettingStool = entity.ItemSettingStool,
                Notes = entity.Notes
            };
        }

        public async Task<TestitemsettingDto> UpdateAsync(int id, UpdateTestitemsettingDto input)
        {
            var entity = await _testitemsettingsRepository.GetAsync(id);
            entity.ItemSettingsCode = input.ItemSettingsCode;
            entity.ItemSettingsName = input.ItemSettingsName;
            entity.ItemSettingsType = input.ItemSettingsType;
            entity.ItemSettingStool = input.ItemSettingStool;
            entity.Notes = input.Notes;
            await _testitemsettingsRepository.UpdateAsync(entity);
            return new TestitemsettingDto
            {
                Id = entity.Id,
                ItemSettingsCode = entity.ItemSettingsCode,
                ItemSettingsName = entity.ItemSettingsName,
                ItemSettingsType = entity.ItemSettingsType,
                ItemSettingStool = entity.ItemSettingStool,
                Notes = entity.Notes
            };
        }

        public async Task DeleteAsync(int id)
        {
            await _testitemsettingsRepository.DeleteAsync(id);
        }

        public async Task BatchDeleteAsync(List<int> ids)
        {
            await _testitemsettingsRepository.DeleteAsync(x => ids.Contains(x.Id));
        }

        public async Task<byte[]> ExportAsync()
        {
            var list = await _testitemsettingsRepository.GetListAsync();
            var sb = new StringBuilder();
            sb.AppendLine("Id,ItemSettingsCode,ItemSettingsName,ItemSettingsType,ItemSettingStool,Notes");
            foreach (var item in list)
            {
                sb.AppendLine($"{item.Id},{item.ItemSettingsCode},{item.ItemSettingsName},{item.ItemSettingsType},{item.ItemSettingStool},{item.Notes}");
            }
            return Encoding.UTF8.GetBytes(sb.ToString());
        }
    }
}
